﻿using GodSystemBLL;
using GodSystemModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GodSystem.FormChildren
{
    public partial class FormUserRole : Form
    {
        private static FormUserRole formUserRole = null;
        private static int userId;
        public FormUserRole()
        {
            InitializeComponent();
            InitData();
        }

        public static FormUserRole CreateInstance()
        {
            if (formUserRole == null || formUserRole.IsDisposed)
            {
                formUserRole = new FormUserRole();
            }
            return formUserRole;
        }

        private void InitData()
        {
            int startLocationX = 20;
            int startLocationY = 20;
            int sizeX = 200;
            int sizeY = 20;
            int sep = 20;

            UsersModel user = FormUserList.usersModel;
            label_UserInfo.Text = "用户：" + user.Name;
            userId = user.Id;
            UsersBLL usersBLL = new UsersBLL();
            Dictionary<int, string> userRole =  usersBLL.GetUserRoleByUserId(user.Id);
            RolesBLL rolesBLL = new RolesBLL();
            List<RolesModel> roleList = rolesBLL.GetRolesList();
         
            for(int i = 0; i < roleList.Count; i++)
            {
                int newstartLocationY = startLocationY + (sizeY + sep) * i;
                CheckBox checkBox = new CheckBox();
                checkBox.Anchor = AnchorStyles.Left | AnchorStyles.Top;
                checkBox.Location = new Point(startLocationX, newstartLocationY);
                checkBox.Size = new Size(sizeX, sizeY);
                checkBox.Text = roleList[i].RoleName + "(" + roleList[i].Remark + ")";
                checkBox.Tag = roleList[i];
                if (userRole.ContainsValue(roleList[i].Id.ToString()))
                {
                    checkBox.Checked = true;
                }
                checkBox.Name = "RoleName-" + roleList[i].Id;
                panel_RoleList.Controls.Add(checkBox);
            }
        }

        private void button_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
        }

        private void button_RoleUpdate_Click(object sender, EventArgs e)
        {
            List<int> roleId = new List<int>();
            foreach(Control control in panel_RoleList.Controls)
            {
                if(control is CheckBox)
                {
                    CheckBox checkBox = (CheckBox)control;
                    if (checkBox.Checked)
                    {
                        RolesModel role = (RolesModel)checkBox.Tag;
                        roleId.Add(role.Id);
                    }
                }
            }

            UsersBLL usersBLL = new UsersBLL();
            usersBLL.UpdateUserRole(userId, roleId);
            MessageBox.Show("角色更新成功");
        }
    }
}
